package com.tusuo.stds.ui.school;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.tusuo.stds.R;
import com.tusuo.stds.rest.AreaList;
import com.tusuo.stds.rest.UserInfo;

import java.util.List;

/**
 * @author J.Huang
 * @version 1.0
 *
 */
public class AreaListAdapter extends BaseAdapter {

    private List<AreaList.Area> dataList;
    private LayoutInflater layoutInflater;
    private Context mContext;
    private UserInfo mUserInfo;
    private int mLevel;

    public AreaListAdapter(Context context, List<AreaList.Area> dataList, UserInfo userInfo, int level) {
        this.mContext = context;
        this.dataList = dataList;
        this.layoutInflater = LayoutInflater.from(context);
        mUserInfo = userInfo;
        mLevel = level;
    }

    private final class Component {
        TextView tvName;
        TextView tvSelected;
    }

    @Override
    public int getCount() {
        return dataList.size();
    }

    @Override
    public AreaList.Area getItem(int position) {
        return dataList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Component component = null;
        if (convertView == null) {
            component = new Component();
            // 获得组件，实例化组件
            convertView = layoutInflater.inflate(R.layout.area_list_item, null);

            component.tvName = (TextView) convertView.findViewById(R.id.area_list_item_tv_name);
            component.tvSelected = (TextView) convertView.findViewById(R.id.area_list_item_tv_selected);

            convertView.setTag(component);
        } else {
            component = (Component) convertView.getTag();
        }

        AreaList.Area area = dataList.get(position);

        // 绑定数据
        component.tvName.setText(area.Name);

        switch (mLevel) {
            case 1:
                if (mUserInfo.ProvinceId == area.Id) {
                    component.tvSelected.setVisibility(View.VISIBLE);
                } else {
                    component.tvSelected.setVisibility(View.GONE);
                }
                break;
            case 2:
                if (mUserInfo.CityId == area.Id) {
                    component.tvSelected.setVisibility(View.VISIBLE);
                } else {
                    component.tvSelected.setVisibility(View.GONE);
                }
                break;
            case 3:
                if (mUserInfo.AreaId == area.Id) {
                    component.tvSelected.setVisibility(View.VISIBLE);
                } else {
                    component.tvSelected.setVisibility(View.GONE);
                }
                break;
            case 4:
                if (mUserInfo.SchoolId == area.Id) {
                    component.tvSelected.setVisibility(View.VISIBLE);
                } else {
                    component.tvSelected.setVisibility(View.GONE);
                }
                break;

        }


        return convertView;
    }
}
